package edu.bbu.commonproject.desktop.gui.view.swing.utils;

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

/**
 * The textured panel class, the background is filld with a texture.
 */
public class TexturedPanel extends JPanel
{	
	/** The image texture. */
	Image texture;
	
	/** The texture width. */
	int textureWidth = 0;
	
	/** The texture height. */
	int textureHeight = 0;
	
	/**
	 * The constructor.
	 */
	public TexturedPanel()
	{
		texture = null;
	}
	
	/**
	 * The constructor.
	 * @param texture the image texture.
	 */
	public TexturedPanel(Image texture)
	{
		setTexture(texture);
	}
	
	/**
	 * Sets the texture.
	 * @param texture the thexture image.
	 */
	public void setTexture(Image texture)
	{
		this.texture = texture;
		
		this.textureWidth = texture.getWidth(null);
		this.textureHeight = texture.getHeight(null);
	}
	
	/**
	 * The overrided paint method.
	 */
	protected void paintComponent(Graphics g)
	{
		if(texture != null)
		{
			int widthChunks = this.getWidth() / textureWidth;
			int heightChunks = this.getHeight() / textureHeight;
			
			for(int i = 0; i <= heightChunks; i++)
			{
				for(int j = 0; j <= widthChunks; j++)
				{
					g.drawImage(texture, j * textureWidth, i * textureHeight, null);
				}
			}
		}
		
	}
}
